﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CarbonCalculations : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        grid.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler(grid_NeedDataSource);
        download.DataBinding += new EventHandler(download_DataBinding);
    }

    void download_DataBinding(object sender, EventArgs e)
    {
        download.DataSource = GetData();
    }

    void grid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        grid.DataSource = GetData();
    }

    /// <summary>
    /// USed to hold data as some proplem with anon types that couldnt resolve nullable datetime and nullable floats with inline if statement
    /// </summary>
    public class TmpDataItem
    {
        internal TmpDataItem(string schoolName, int pupils, DateTime created1, float electricityPerYear1, float wastePerYear1, ClimateChangeChampionsData.CarbonCalculation footprint2)
        {
            SchoolName = schoolName;
            Pupils = pupils;
            Created1 = created1;
            ElectricityPerYear1 = electricityPerYear1;
            WastePerYear1 = wastePerYear1;
            if (footprint2 != null)
            {
                Created2 = footprint2.CreatedDateTime;
                ElectricityPerYear2 = footprint2.TonnesCO2PerSchoolYear;
                WastePerYear2 = footprint2.TotalWasteCO2PerYear;
            }
        }
        public string SchoolName { get; private set; }
        public int Pupils { get; private set; }
        public DateTime Created1 { get; private set; }
        public float ElectricityPerYear1 { get; private set; }
        public float WastePerYear1 { get; private set; }
        public DateTime? Created2 { get; private set; }
        public float? ElectricityPerYear2 { get; private set; }
        public float? WastePerYear2 { get; private set; }
    }

    private object GetData()
    {
        ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
        var data = from footprint1 in context.CarbonCalculations
                   where footprint1.Audit == 1
                   join schoolname in context.UserCustomProperties on new { Id = footprint1.UserId, Name = "School Name" } equals new { Id = schoolname.UserId, Name = schoolname.Name }
                   join fp2 in context.CarbonCalculations on new { UserId = footprint1.UserId, Audit = 2 } equals new { UserId = fp2.UserId, Audit = fp2.Audit } into ljfp2
                   from footprint2 in ljfp2.DefaultIfEmpty()
                   select new TmpDataItem(schoolname.Value, footprint1.NoPupils, footprint1.CreatedDateTime, footprint1.TonnesCO2PerSchoolYear, footprint1.TotalWasteCO2PerYear, footprint2)
                   ;
                   
        return data;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
